import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:lcapp/api/interface/user_api.dart';
import 'package:lcapp/api/request/response_data.dart';
import 'package:lcapp/config/const_config.dart';
import 'package:lcapp/data/zixun_data_entity.dart';
import 'package:lcapp/model/refresh/refresh.dart';
import 'package:lcapp/page/min/min_zixun_item.dart';

class MinCollectionPage extends StatefulWidget {
  final bool isMine;
  final int uid;
  const MinCollectionPage({Key? key, required this.isMine, required this.uid}) : super(key: key);

  @override
  _MinCollectionPageState createState() => _MinCollectionPageState();
}

class _MinCollectionPageState extends State<MinCollectionPage> {

  List<ZixunDataEntity> zixuns = [];
  int pageNum = 1;
  int pageSize = 10;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getZixun();
  }

  getZixun()async{
    ResponseData res = await UserAPI.getMinCollections(widget.uid, pageNum, pageSize);
    if(res.code==200){
      if(pageNum==1){
        zixuns.clear();
      }
      List arr = res.data??[];
      for (var e in arr) {
        ZixunDataEntity entity = ZixunDataEntity().fromJson(e);
        entity.layout = 3;
        zixuns.add(entity);
      }
    }

    if(mounted){setState(() {
    });}
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: bgColor,
      child: EasyRefresh(
        header: myRefreshHeader(),
        footer: myRefreshFooter(),
        onRefresh: ()async{
          pageNum=1;
          getZixun();
        },
        onLoad: ()async{
          pageNum++;
          getZixun();
        },
        child: ListView.builder(
            padding: EdgeInsets.zero,
            shrinkWrap: true,
            itemCount: zixuns.length,
            itemBuilder: (BuildContext context, int i) {
              return MinZixunItem(entity: zixuns[i],clickFun: (){
                Navigator.of(context).pushNamed('ZixunDetailPage',arguments: zixuns[i].id).then((value){
                  pageNum=1;
                  getZixun();
                });
              },);
            }),
      ),
    );
  }
}
